package io.litchi.auth.Infrastructure.handler;

import io.litchi.auth.Infrastructure.securityentity.LitchiUser;
import io.litchi.auth.application.service.AuthService;
import io.litchi.auth.domain.entity.auth.User;
import io.litchi.common.exception.LitchiException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * description: 自定义处理登录用户信息service
 *
 * @author helang
 * @since 2025-09-10
 */
@Service
@AllArgsConstructor
@Slf4j
public class LitchiUserDetailService implements UserDetailsService {
    private AuthService authService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User currentLoginUser = authService.findByUsername(username, User.class)
                .orElseThrow(() -> new LitchiException("账号未注册"));
        log.info("username:{}",username);
        return LitchiUser.builder()
                .id(currentLoginUser.getId())
                .username(currentLoginUser.getUsername())
                .password(currentLoginUser.getPassword())
                .build();
    }
}